package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
/**
 * 定时任务类
 */
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 处理超过十五分钟未支付订单，将状态改为取消，每一分钟执行一次检查
     */
    @Scheduled(cron = "0 * * * * ?")
    public void PaymentTimeoutTask() {
        LocalDateTime nowTime = LocalDateTime.now();
        List<Orders> orders = orderMapper.getByOrderTime(nowTime);
        for (int i = 0; i < orders.size(); i++) {
            orders.get(i).setCancelTime(LocalDateTime.now());
            orderMapper.ChangeTimeOutStatus(orders.get(i));
        }
        log.info("超时订单处理{}", orders);
    }

    /**
     * 检查一直处于派送中的订单，每天凌晨一点检查
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void DeliveringTimeOut() {
        List<Orders> orders = new ArrayList<>();
        orders = orderMapper.getBystatus(Orders.DELIVERY_IN_PROGRESS);
        for (int i = 0; i < orders.size(); i++) {
            orders.get(i).setDeliveryTime(LocalDateTime.now());
            orderMapper.ChangeDeliveringTimeOutStatus(orders.get(i));
        }
        log.info("派送中状态订单处理{}", orders);
    }
}
